package com.example.controller;

import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.User;
import com.example.exception.CustomerException;
import com.example.service.AdminService;
import com.example.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class WebController {
    @Resource
    AdminService adminService;
    @Resource
    UserService userService;
    @GetMapping("/")
    public Result index() {
        int a = 1/0;
        Result result = new Result();
        return result.success("hello world");
    }
    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        Result result = new Result();
        Account adminInfo= new Account();
        if(account.getRole().equals("ADMIN")){
            adminInfo=adminService.login(account.getUsername(), account.getPassword());
        }else if(account.getRole().equals("USER")){
            adminInfo=userService.login(account.getUsername(), account.getPassword());
        }
        if (adminInfo!=null) {
            return result.success(adminInfo);
        } else {
            throw new CustomerException("用户名或密码错误");
        }
    }
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        Result result = new Result();
        userService.register(user);
        return result.success();
    }
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account){
        if(account.getRole().equals("ADMIN")){
            adminService.updatePassword(account);
        }
        else if (account.getRole().equals("USER")) {
            userService.updatePassword(account);
        }
        return Result.success();
    }
}
